package com.bang.study.day01.queue;

import com.bang.study.day01.list.Array;

/**
 * @Auther: Bang
 * @Date: 2019/9/9 23:14
 * @Description: 基于数组实现的队列
 */
public class ArrayQueue<E> implements Queue<E> {

    private Array<E> data;


    public ArrayQueue() {
        data = new Array<>();
    }

    public ArrayQueue(int capacity) {
        data = new Array<>(capacity);
    }

    @Override
    public int getSize() {
        return data.getSize();
    }

    @Override
    public boolean isEmpty() {
        return data.isEmpty();
    }

    @Override
    public void enqueue(E e) {
        data.addLast(e);
    }

    @Override
    public E dequeue() {
        return data.removeFirst();
    }

    @Override
    public E getFront() {
        return data.getFirst();
    }

    @Override
    public String toString() {
        StringBuffer sb = new StringBuffer();
        sb.append("front [");
        for (int i = 0; i < data.getSize(); i++) {
            sb.append(data.get(i));
            if (i != data.getSize() - 1) {
                sb.append(",");
            }
        }
        sb.append("] tail");

        return "ArrayQueue{" +
                "data=" + sb.toString() +
                '}';
    }

    public static void main(String[] args) {
        ArrayQueue<Integer> queue = new ArrayQueue<>();
        for(int i = 0 ; i < 10 ; i ++){
            queue.enqueue(i);
            System.out.println(queue);
            if(i % 3 == 2){
                queue.dequeue();
                System.out.println(queue);
            }
        }
    }
}
